/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.inventory.service.impl;

import me.zhengjie.suli.inventory.domain.TallyTasks;
import me.zhengjie.suli.inventory.service.InventoryService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.inventory.repository.TallyTasksRepository;
import me.zhengjie.suli.inventory.service.TallyTasksService;
import me.zhengjie.suli.inventory.service.dto.TallyTasksDto;
import me.zhengjie.suli.inventory.service.dto.TallyTasksQueryCriteria;
import me.zhengjie.suli.inventory.service.mapstruct.TallyTasksMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Allen Gao
* @date 2024-05-22
**/
@Service
@RequiredArgsConstructor
public class TallyTasksServiceImpl implements TallyTasksService {

    private final TallyTasksRepository tallyTasksRepository;
    private final TallyTasksMapper tallyTasksMapper;

    private final InventoryService inventoryService;

    @Override
    public Map<String,Object> queryAll(TallyTasksQueryCriteria criteria, Pageable pageable){
        Page<TallyTasks> page = tallyTasksRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tallyTasksMapper::toDto));
    }

    @Override
    public List<TallyTasksDto> queryAll(TallyTasksQueryCriteria criteria){
        return tallyTasksMapper.toDto(tallyTasksRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TallyTasksDto findById(String id) {
        TallyTasks tallyTasks = tallyTasksRepository.findById(id).orElseGet(TallyTasks::new);
        ValidationUtil.isNull(tallyTasks.getId(),"TallyTasks","id",id);
        return tallyTasksMapper.toDto(tallyTasks);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TallyTasksDto create(TallyTasks resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return tallyTasksMapper.toDto(tallyTasksRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TallyTasks resources) {
        TallyTasks tallyTasks = tallyTasksRepository.findById(resources.getId()).orElseGet(TallyTasks::new);
        ValidationUtil.isNull( tallyTasks.getId(),"TallyTasks","id",resources.getId());
        tallyTasks.copy(resources);
        tallyTasksRepository.save(tallyTasks);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            tallyTasksRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TallyTasksDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TallyTasksDto tallyTasks : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("四层及以下的箱数", tallyTasks.getQty());
            map.put("物料编码", tallyTasks.getMaterialCode());
            map.put("物料名称", tallyTasks.getMaterialName());
            map.put("创建用户", tallyTasks.getCreateBy());
            map.put("修改用户", tallyTasks.getUpdateBy());
            map.put("创建日期", tallyTasks.getCreateTime());
            map.put("修改日期", tallyTasks.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int continueTallyTasks() {
        return inventoryService.generateTallyTasks();
    }
}